package com.example.recyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    Button btn_adddata;
    Button btn_grideview;
    Button btn_insertview;
    Button btn_deleteview;
    RecyclerView mrecyclerview;
    private MyRecyclerViewAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();

        //线性布局
        //纵向排列
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        linearLayoutManager.setReverseLayout(false);

        //横向排列 并设置数据排列方式(reverselayout 第三个参数)
        //LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        //linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //linearLayoutManager.setReverseLayout(false);

        mrecyclerview.setLayoutManager(linearLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(this,mrecyclerview);
        mAdapter.setClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                    Toast.makeText(MainActivity.this,"第"+(position+=1)+"张图片被点击了",Toast.LENGTH_SHORT).show();
            }
        });
        mrecyclerview.setAdapter(mAdapter);

    }

    private void initview() {
        btn_adddata = findViewById(R.id.btn_adddata);
        btn_adddata.setOnClickListener(this);

        btn_grideview = findViewById(R.id.btn_selectview);
        btn_grideview.setOnClickListener(this);

        btn_insertview = findViewById(R.id.btn_insertview);
        btn_insertview.setOnClickListener(this);

        btn_deleteview = findViewById(R.id.btn_deleteview);
        btn_deleteview.setOnClickListener(this);

        mrecyclerview = findViewById(R.id.recyclerview);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_adddata:
                adddata();
            case R.id.btn_selectview:
                selectview();
            case R.id.btn_insertview:
                insertview();
            case R.id.btn_deleteview:
                deleteview();
        }
    }

    /**
     * 删除item
     */
    private void deleteview() {
    }

    /**
     * 插入item
     */
    private void insertview() {
    }

    /**
     * 切换布局
     */
    private void selectview() {
        //从线性布局切换为网格布局
        if (mrecyclerview.getLayoutManager().getClass() == LinearLayoutManager.class) {
            GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
            mrecyclerview.setLayoutManager(gridLayoutManager);
        }else if(mrecyclerview.getLayoutManager().getClass() == GridLayoutManager.class){
            StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
            mrecyclerview.setLayoutManager(staggeredGridLayoutManager);
        }else {
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
            mrecyclerview.setLayoutManager(linearLayoutManager);
        }
    }

    /**
     * 添加数据
     */
    private void adddata() {
        Log.d(TAG, "adddata: 点击了adddata");
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            String s = "第" + i + "条数据";
            data.add(s);
        }
        //把文本数据放入adapter
        mAdapter.setDatasource(data);
        Toast.makeText(MainActivity.this, "点击了添加数据", Toast.LENGTH_SHORT).show();
    }
}
